package com.pro.staffmanage.resolvers;

import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * @Author: Haotian Cui
 * @Date: 2022/7/22 - 07 - 22 - 21:58
 * @Description: com.pro.staffmanage.config
 * @Project: staffmanage
 * @version: 1.0
 */
public class MyLocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //  Get the language parameter in the request
        String lang = request.getParameter("lang");
        //  default locale
        Locale locale = Locale.getDefault();

        //  If Param-lang existed , new local(lang.split[0], lang.split[1]);
        //  Constructor Params:
        //      1. lang.split[0] is language;
        //      2. lang.split[1] is zone;
        if(!StringUtils.isEmpty(lang)){
            String[] split = lang.split("_");
            locale = new Locale(split[0], split[1]);
        }

        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
